package com.xinqi.modules.sys.dict.controller.web;

import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.sys.dict.dto.req.DictDocumentCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictDocumentQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictDocumentUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictDocumentResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictDocumentTreeResultDTO;
import com.xinqi.modules.sys.dict.service.DictDocumentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文档分类字典表WEB控制层
 *
 * @author: gzy
 * @date: 2023/05/28
 */
@Tag(name = "Web-文档分类字典表", description = "文档分类字典表Web接口")
@RestController
@RequestMapping("/web/dict-document")
@RequiredArgsConstructor
public class DictDocumentWebController {
    private final DictDocumentService dictDocumentService;

    /**
     * 文档分类字典表-分页
     */
    @Operation(summary = "文档分类字典表-分页")
    @GetMapping("/page")
    public R<PageInfo<DictDocumentResultDTO>> page(@ParameterObject DictDocumentQueryDTO dto) {
        PageInfo<DictDocumentResultDTO> page = dictDocumentService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 文档分类字典表-详情
     */
    @Operation(summary = "文档分类字典表-详情")
    @GetMapping("/{id}")
    public R<DictDocumentResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        DictDocumentResultDTO dto = dictDocumentService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增文档分类字典表
     */
    @Operation(summary = "新增文档分类字典表")
    @PostMapping
    public R<Long> insert(@RequestBody DictDocumentCreateDTO dto) {
        return R.data(dictDocumentService.insert(dto));
    }

    /**
     * 修改文档分类字典表
     */
    @Operation(summary = "修改文档分类字典表")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody DictDocumentUpdateDTO dto) {
        dto.setId(id);
        return R.data(dictDocumentService.updateById(dto));
    }

    /**
     * 删除文档分类字典表
     */
    @Operation(summary = "删除文档分类字典表")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(dictDocumentService.deleteById(id));
    }

    /**
     * 文档分类字典表-列表
     */
    @Operation(summary = "文档分类字典表-列表")
    @GetMapping("/findAll")
    public R<List<DictDocumentResultDTO>> findAll(@ParameterObject DictDocumentQueryDTO dto) {
        List<DictDocumentResultDTO> list = new ArrayList<>();
        if (Objects.isNull(dto.getParentCode()) && Objects.isNull(dto.getParentId())) {
            list = dictDocumentService.findAll();
        } else {
            if (Objects.nonNull(dto.getParentId())) {
                list = dictDocumentService.findSubListByParentId(dto.getParentId());
            }
            if (Objects.nonNull(dto.getParentCode())) {
                list = dictDocumentService.findSubListByParentCode(dto.getParentCode());
            }
        }
        return R.data(list);
    }

    /**
     * 文档分类字典表-树结构列表
     */
    @Operation(summary = "文档分类字典表-树结构列表")
    @GetMapping("/findAllTree")
    public R<List<DictDocumentTreeResultDTO>> findAllTree() {
        List<DictDocumentTreeResultDTO> list = dictDocumentService.findAllTree();
        return R.data(list);
    }

    /**
     * 文档分类字典表-树结构列表
     */
    @Operation(summary = "文档分类字典表-通过parentId获取下级字典")
    @GetMapping("/findSubListByParentId/{parentId}")
    public R<List<DictDocumentResultDTO>> findSubListByParentId(@Parameter(description = "parentId", example = "100001") @PathVariable("parentId") Long parentId) {
        List<DictDocumentResultDTO> list = dictDocumentService.findSubListByParentId(parentId);
        return R.data(list);
    }

    /**
     * 文档分类字典表-通过parentCode获取下级字典
     */
    @Operation(summary = "文档分类字典表-通过parentCode获取下级字典")
    @GetMapping("/findSubListByParentCode/{parentCode}")
    public R<List<DictDocumentResultDTO>> findSubListByParentCode(@Parameter(description = "字典父级parentCode", example = "100001") @PathVariable("parentCode") String parentCode) {
        List<DictDocumentResultDTO> list = dictDocumentService.findSubListByParentCode(parentCode);
        return R.data(list);
    }
}
